import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {HttpService} from '../http.service';
import {ColumnItem, Course, Student, Studentcourse} from '../student';

@Component({
  selector: 'app-studentgrade',
  templateUrl: './studentgrade.component.html',
  styleUrls: ['./studentgrade.component.css']
})
export class StudentgradeComponent implements OnInit {
  stuid: number = Number(this.route.snapshot.paramMap.get('stuid'));
  courseinfo: Course[] = [];
  studentcourses: Studentcourse[] = [];
  loading = true ;
  DisplayData: Studentcourse[] = [] ;
  DisplayData2: Studentcourse[] = [];
  Gradepoint = 0;
  creditsum = 0;
  GPA: number;
  student: Student = {
    birthday : '' ,
    class : '' ,
    major : '' ,
    name  : '' ,
    native_place : '' ,
    sex : '' ,
    stuid : null ,
    time_of_enrollment : '' ,
  };
  // courseid: string;
  // len: number;
  constructor(private cdf: ChangeDetectorRef,
              public router: Router,
              private httpService: HttpService,
              private route: ActivatedRoute) { }

  listOfColumns: ColumnItem[] = [
    {
      name: '课程号',
      sortFn: (a: Studentcourse, b: Studentcourse) => a.Courseid.localeCompare(b.Courseid)
    },
    {
      name: '课程名称',
      sortFn: (a: Studentcourse, b: Studentcourse) => a.coursename.localeCompare(b.coursename)
    },
    {
      name: '学分'
    },
    {
      name : '课程类型',
      // filterMultiple : true,
      listOfFilter: [
        { text: '必修课', value: '必修课'},
        { text: '选修课', value: '选修课'},
      ],
      filterFn: (list: string[], item: Studentcourse) => list.some(name => item.type.indexOf(name) !== -1)
    },
    {
      name : '成绩',
      sortOrder: null,
      sortFn: (a: Studentcourse, b: Studentcourse) => a.score - b.score
    },
    {
      name: '绩点'
    }
  ];
  getcouresinfobyID() {
    return this.httpService.course_select_stuid(this.stuid).subscribe(
      (res) => {
        res.data.forEach(row => {
          this.courseinfo.push( {
              courseid : row.courseid,
              score : row.score,
              stuid : row.stuid,
            }
          );
        });
        console.log(this.courseinfo);
        for ( const i in this.courseinfo) {
          this.httpService.getCourseDetail(this.courseinfo[i].courseid).subscribe(
            result => {
              console.log(this.courseinfo[i].courseid);
              this.studentcourses.push({
                Courseid: result.data.courseid,
                coursename: result.data.coursename,
                stuid: this.courseinfo[i].stuid,
                credit: result.data.credit,
                type: result.data.type,
                score: this.courseinfo[i].score,
                gradepoint: this.courseinfo[i].score / 10 - 5,
              });
              if (this.courseinfo.length === this.studentcourses.length) {
                this.DisplayData = this.studentcourses;
                this.DisplayData2 = this.studentcourses;
              }
              this.loading = false;
            });
        }
        setTimeout(() => {
          this.getGPA();
          this.cdf.markForCheck();
          this.cdf.detectChanges();
        }, 2000);
      }
    );
  }
  getGPA(): number {
    // tslint:disable-next-line:forin
    for (const i in this.DisplayData2) {
      this.Gradepoint = this.Gradepoint + ((this.DisplayData2[i].score) / 10 - 5) * this.DisplayData2[i].credit;
      this.creditsum = this.creditsum + this.DisplayData2[i].credit;
    }
    this.GPA = this.Gradepoint / this.creditsum;
    return this.GPA;
  }
  getStudentbyID() {
    this.httpService.getStudentDetail(this.stuid).subscribe(
      res => this.student = {
        birthday : res.data.birthday,
        class : res.data.class,
        major : res.data.major,
        name : res.data.name,
        native_place : res.data.native_place,
        sex : res.data.sex,
        stuid : res.data.stuid,
        time_of_enrollment : res.data.time_of_enrollment
      }
    );
  }
  ngOnInit(): void {
    this.getStudentbyID();
    this.getcouresinfobyID();
  }
}
